package com.example.smartplay.controller;


import com.example.smartplay.entity.Notice;
import com.example.smartplay.entity.Result;
import com.example.smartplay.mapper.NoticeMapper;
import com.example.smartplay.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    //查询所有通知
    @GetMapping
    public Result list(){
        //调用service查询通知数据
        List<Notice> noticelist = noticeService.list();
        return Result.success(noticelist);

    }

    //分页查询通知
    @GetMapping("/getnotice/{pageNum}/{pageSize}")
    public Result getnotice(
            @RequestParam(value = "keyword",required = false) String keyword,
            @PathVariable("pageNum" ) Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        log.info("分页查询通知:{},{}",pageNum,pageSize);
        return noticeService.getnotice(keyword, pageNum,pageSize);
    }
    //新增通知

    @PostMapping
    public Result add(@RequestBody Notice notice){
        log.info("新增通知:{}",notice);
        //调用service
        noticeService.add(notice);
        return Result.success();
    }
    //删除通知
/*    @DeleteMapping
    public Result delete( Integer id){
        System.out.println(id);
        log.info("根据ID删除通知:{}",id);
        noticeService.delete(Collections.singletonList(id));
        return Result.success();

    }*/


    //批量删除通知
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("根据ID删除通知:{}",ids);

        //调用service
        noticeService.delete(ids);

        return Result.success();
    }
    //查询
    @GetMapping("/{id}")
    public Result find( @PathVariable("id") Integer id){
        Notice notice =  noticeService.find(id);
        return Result.success(notice);
    }
    //查询（回显）后修改
    @PutMapping
    public Result update2(@RequestBody Notice notice){
        noticeService.update2(notice);
        return Result.success();
    }
}
